//
//  XMN_LikeViewController.m
//  B段项目
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_LikeViewController.h"
#import "XMN_ReadDetailModel.h"
#import "XMN_LikeCell.h"
#import "XMN_ReadDetailViewController.h"
#import "XMN_ReadViewController.h"

@interface XMN_LikeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)NSArray *dataArray;
@property(nonatomic, strong)UITableView *tableView;
@end

@implementation XMN_LikeViewController

-(void)loadView{
    [super loadView];
    
    //初始化数组
    _dataArray = [NSArray array];
    
    _manager = [CoreDataManager shareManager];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的喜欢";
}

#pragma mark - tableView的相关设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    XMN_LikeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XMN_LikeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    cell.titleLabel.text = [[_dataArray objectAtIndex:indexPath.row] title];
    cell.contentLabel.text = [[_dataArray objectAtIndex:indexPath.row] content];
    [cell.covImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_dataArray objectAtIndex:indexPath.row] image]]]];
    [cell.covImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_dataArray objectAtIndex:indexPath.row] image]]] placeholderImage:[UIImage imageNamed:@"XMN_Read_Cell"]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110 * FitHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XMN_ReadDetailViewController *readVC = [[XMN_ReadDetailViewController alloc]init];
    [self.navigationController pushViewController:readVC animated:YES];
    readVC.idString = [[_dataArray objectAtIndex:indexPath.row] cid];
    readVC.titleString = [[_dataArray objectAtIndex:indexPath.row] title];
}

-(void)viewWillAppear:(BOOL)animated{
    NSFetchRequest *fetchrequest = [NSFetchRequest fetchRequestWithEntityName:@"XMN_ReadDetailModel"];
    //查找条件
    NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
    [fetchrequest setPredicate:predicate];
    //开始查询
    _dataArray = [_manager.managedObjectContext executeFetchRequest:fetchrequest error:nil];
    
    if (_dataArray.count != 0) {
        //初始化tableView
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView NightWithType:UIViewColorTypeNormal];
        [self.view addSubview:_tableView];
    }else{
        DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"您还没有喜欢的收藏" contentText:@"快去收藏吧" leftButtonTitle:@"返回" rightButtonTitle:@"前往"];
        [alert show];
        alert.rightBlock = ^(){
            XMN_ReadViewController *readVc = [[XMN_ReadViewController alloc]init];
            [self.navigationController pushViewController:readVc animated:YES];
        };
        alert.leftBlock = ^(){
            //显示左侧menu菜单
            [(DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController showLeftController:YES];
        };
    }
    
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
